package com.zero.basic.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository("sqlListBaseDao")
public class SqlListBaseDao<T> extends DefaultBaseDao<T> {

	/**
	 *
	 * @param sql
	 * @param args
	 * @param alias
	 * @param clz
	 * @param hasEntity
	 *            if entity exists in the hibernate
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <N extends Object> List<N> listBySql(String sql, Object[] args,
			Map<String, Object> alias, Class<?> clz, boolean hasEntity,
			boolean transform) {
		sql = initSort(sql);
		SQLQuery sq = getSession().createSQLQuery(sql);
		setAliasParameter(sq, alias);
		setParameter(sq, args);
		if (transform) {
			if (hasEntity) {
				sq.addEntity(clz);
			} else
				sq.setResultTransformer(Transformers.aliasToBean(clz));
		}
		return sq.list();
	}

	public <N extends Object> List<N> listBySql(String sql, Object arg,
			Class<?> clz, boolean hasEntity, boolean transform) {
		return this.listBySql(sql, new Object[] { arg }, null, clz, hasEntity,
				transform);
	}

	public <N extends Object> List<N> listBySql(String sql, Class<?> clz,
			boolean hasEntity, boolean transform) {
		return this.listBySql(sql, null, null, clz, hasEntity, transform);
	}

	public <N extends Object> List<N> listBySql(String sql, Object[] args,
			Class<?> clz, boolean hasEntity, boolean transform) {
		return this.listBySql(sql, args, null, clz, hasEntity, transform);
	}

	public <N extends Object> List<N> listByAliasSql(String sql,
			Map<String, Object> alias, Class<?> clz, boolean hasEntity,
			boolean transform) {
		return this.listBySql(sql, null, alias, clz, hasEntity, transform);
	}

}